﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

// autoCAD Hander
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;

namespace _02AddEntityTools
{
    public static partial class AddEntityTools
    {
        //public ObjectId addEntityToModelSpace(Database db, Entity ent)
        //{
        //    // 声明ObjectID，方便修改图像属性
        //    ObjectId entId = ObjectId.Null;
        //    // 开启事务处理
        //    using (Transaction trans = db.TransactionManager.StartTransaction())
        //    {
        //        // 打开块表
        //        BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
        //        // 打开块表记录
        //        BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
        //        // 添加图形到块表记录
        //        entId = btr.AppendEntity(ent);
        //        // 更新数据库
        //        trans.AddNewlyCreatedDBObject(ent, true);
        //        // 事务提交
        //        trans.Commit();    
        //    }
        //    return entId;
        //}

        /// <summary>
        ///  将图形载入到图形数据库
        /// </summary>
        /// <param name="db">图形数据库</param>
        /// <param name="ent">图形对象</param>
        /// <returns>图形的ObjectId</returns>
        public static ObjectId addEntityToModelSpace(this Database db, Entity ent)
        {
            // 声明ObjectID，方便修改图像属性
            ObjectId entId = ObjectId.Null;
            // 开启事务处理
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                // 打开块表
                BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
                // 打开块表记录
                BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                // 添加图形到块表记录
                entId = btr.AppendEntity(ent);
                // 更新数据库
                trans.AddNewlyCreatedDBObject(ent, true);
                // 事务提交
                trans.Commit();
            }
            return entId;
        }
        /// <summary>
        /// 将多个图元对象载入到图形数据路
        /// </summary>
        /// <param name="db">图形数据路</param>
        /// <param name="ent">图形对象，可变参数</param>
        /// <returns>图形的ObjectId 数组</returns>
        public static ObjectId[] addEntityToModelSpace(this Database db, params Entity[] ent) 
        {
            // 声明ObjectID，方便修改图像属性
            ObjectId[] entId =new ObjectId[ent.Length];
            // 开启事务处理
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                // 打开块表
                BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
                // 打开块表记录
                BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                
                for (int i = 0; i < ent.Length; i++)
                {
                    // 添加图形到块表记录
                    entId[i] = btr.AppendEntity(ent[i]);
                    // 更新数据库
                    trans.AddNewlyCreatedDBObject(ent[i], true);
                }
                
                // 事务提交
                trans.Commit();
            }
            return entId;
        }
        /// <summary>
        /// 绘制直线
        /// </summary>
        /// <param name="db">图像数据库</param>
        /// <param name="startPoint">起点坐标</param>
        /// <param name="endPoint">终点坐标</param>
        /// <returns>直线对象的ObjectId</returns>
        public static ObjectId addLineToModelSpace(this Database db, Point3d startPoint, Point3d endPoint)
        {
            return db.addEntityToModelSpace(new Line(startPoint, endPoint));
        }
        /// <summary>
        /// 绘制直线二维    
        /// </summary>
        /// <param name="db">图像数据库</param>
        /// <param name="startPoint">起始点坐标</param>
        /// <param name="length">绘制长度</param>
        /// <param name="degree">与X轴正方向的夹角</param>
        /// <returns>直线对象的ObjectId</returns>
        public static ObjectId addLineToModelSpace(this Database db,
            Point3d startPoint, double length, double degree)
        {
            // 计算终点坐标
            double endX = startPoint.X + length * Math.Cos(degree.degreeToAngle());
            double endY = startPoint.Y + length * Math.Sin(degree.degreeToAngle());
            Point3d endPoint3d = new Point3d(endX, endY, 0);

            return db.addEntityToModelSpace(new Line(startPoint, endPoint3d));
        }
        /// <summary>
        /// 绘制二维圆弧
        /// </summary>
        /// <param name="db">图形数据库</param>
        /// <param name="center">圆弧中心</param>
        /// <param name="radius">圆弧半径</param>
        /// <param name="starAngle">起始角度</param>
        /// <param name="endAngl">终止角度</param>
        /// <returns></returns>
        public static ObjectId addArcToModelSpace(this Database db,
            Point3d center,
            double radius,
            double starAngle,
            double endAngl)
        {
            return db.addEntityToModelSpace(new Arc(center, radius, starAngle.angleTodegree(), endAngl.angleTodegree()));
        }
        /// <summary>
        /// 三点绘制圆弧命令
        /// </summary>
        /// <param name="db">图形数据库</param>
        /// <param name="starPoint">起始点</param>
        /// <param name="onArcPoint">圆弧上一点</param>
        /// <param name="endPoint">终止点</param>
        /// <returns></returns>
        public static ObjectId addArcToModelSpace(this Database db,
            Point3d starPoint,
            Point3d onArcPoint,
            Point3d endPoint)
        {
            // 判断三点是否共线
            if (starPoint.isOnOneLine(onArcPoint, endPoint))
                return ObjectId.Null;
            CircularArc3d cArc1 = new CircularArc3d(starPoint, onArcPoint, endPoint);

            double radius = cArc1.Radius;
            Point3d center = cArc1.Center;

            Vector3d cs = center.GetVectorTo(starPoint);
            Vector3d ce = center.GetVectorTo(endPoint);
            Vector3d xvector = new Vector3d(1, 0, 0);   //X 轴方向

            double startAngle = cs.Y > 0 ? xvector.GetAngleTo(cs) : -xvector.GetAngleTo(cs);
            double endAngle = ce.Y > 0 ? xvector.GetAngleTo(ce) : -xvector.GetAngleTo(ce);

            return db.addEntityToModelSpace(new Arc(center, radius, startAngle, endAngle));
        }
        /// <summary>
        /// 通过圆心、起点、夹角绘制圆弧
        /// </summary>
        /// <param name="db">图形数据库</param>
        /// <param name="centerPoint">圆心</param>
        /// <param name="starPoint">起点</param>
        /// <param name="degree">夹角</param>
        /// <returns></returns>
        public static ObjectId addArcToModelSpace(this Database db,
            Point3d centerPoint,
            Point3d starPoint,
            double degree)
        {
            // 获取半径
            
            // 获取起点角度
            double startAngle = centerPoint.getAngleWithXAsix(starPoint);

            return db.addEntityToModelSpace(new Arc(centerPoint, radius, startAngle, endAngle));
        }

    }
}
